package GUI.lecturerGUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import GUI.MainGUI;
import MsgPackage.GetUnlockedExamsPack;

import javax.swing.SwingConstants;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import controllers.LecturerController;
import entities.ExtractedExam;
import entities.Subject;

@SuppressWarnings("serial")
public class LecturerWindow extends JPanel implements ActionListener {
	// final public static int LOGOUT = 1;

	// final public static int
	private JButton btnNewExam;
	private JButton btnPReports;
	private JButton btnExit;
	private JButton btnCreateQuestion;

	private JLabel Welcome;
	private JButton btnAddDurastionTime;
	private JButton btnCreateExamCode;
	private JButton btnLockExam;
	private LecturerController lecturerCtrl;
	private JButton btnCheckExams;
	private JComboBox cmbBxLockExam;
	private List<ExtractedExam> examLst;

	//
	public LecturerWindow(LecturerController lecturerCtrl) {

		super();
		this.lecturerCtrl = lecturerCtrl;
		initialize();

	}
	/**
	 * initializing the GUI
	 */

	private void initialize() {
		setBorder(new TitledBorder(null, "Lecturer manu: ",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBounds(0, 0, MainGUI.setX, MainGUI.setY);
		setBackground(SystemColor.activeCaption);
		setLayout(null);

		add(getlblWelcome());
		add(getBtnExit());
		add(getReports());
		add(getBtnNewExam());
		add(createNewQustions());
		add(getBtnAddDurestionTime());
		add(getBtnCreateExamCode());
		add(getBtnLockExam());
		add(getCheckExam());
		add(getcmbBxLockExam());
	}

	/*
	 * private Component generateExamCode() {
	 * 
	 * return genetatexam; }
	 */

	private JComboBox getcmbBxLockExam() {
		if (cmbBxLockExam == null) {
			cmbBxLockExam = new JComboBox();
			cmbBxLockExam.setBounds(53, 400, 282, 29);
			cmbBxLockExam.addActionListener(this);
		}

		return cmbBxLockExam;
	}

	private JButton getCheckExam() {
		if (btnCheckExams == null) {
			btnCheckExams = new JButton("Check exams");
			btnCheckExams.setSize(new Dimension(282, 40));
			btnCheckExams.setFont(new Font("Dialog", Font.BOLD, 12));
			btnCheckExams.setBorder(BorderFactory
					.createLineBorder(SystemColor.desktop));
			btnCheckExams.setBounds(53, 437, 282, 40);
			btnCheckExams.addActionListener(this);
		}

		return btnCheckExams;
	}

	private JButton createNewQustions() {
		if (btnCreateQuestion == null) {
			btnCreateQuestion = new JButton("Create new question");
			btnCreateQuestion.setSize(new Dimension(282, 40));
			btnCreateQuestion.setLocation(53, 131);
			btnCreateQuestion.setFont(new Font("Microsoft New Tai Lue ",
					Font.BOLD, 12));
			btnCreateQuestion.setBorder(BorderFactory
					.createLineBorder(SystemColor.desktop));
			btnCreateQuestion.addActionListener(this);
		}

		return btnCreateQuestion;
	}

	private JButton getBtnNewExam() {
		if (btnNewExam == null) {
			btnNewExam = new JButton("Create new exam");
			btnNewExam.setSize(new Dimension(282, 40));
			btnNewExam.setLocation(53, 182);
			btnNewExam
					.setFont(new Font("Microsoft New Tai Lue ", Font.BOLD, 12));
			btnNewExam.setBorder(BorderFactory
					.createLineBorder(SystemColor.desktop));
			btnNewExam.addActionListener(this);
		}
		return btnNewExam;
	}

	private JLabel getlblWelcome() {
		if (Welcome == null) {
			Welcome = new JLabel("WELCOME");
			Welcome.setHorizontalAlignment(SwingConstants.CENTER);
			Welcome.setFont(new Font("Tahoma", Font.PLAIN, 30));
			Welcome.setBackground(SystemColor.inactiveCaptionBorder);
			Welcome.setBounds(-29, 31, 519, 89);
		}
		return Welcome;
	}

	private JButton getReports() {
		if (btnPReports == null) {
			btnPReports = new JButton("Reports");
			btnPReports.setSize(new Dimension(282, 40));
			btnPReports.setLocation(53, 284);
			btnPReports.setFont(new Font("Microsoft New Tai Lue ", Font.BOLD,
					12));
			btnPReports.setBorder(BorderFactory
					.createLineBorder(SystemColor.desktop));
			btnPReports.addActionListener(this);
		}
		return btnPReports;

	}

	private JButton getBtnExit() {
		if (btnExit == null) {
			btnExit = new JButton("EXIT");
			btnExit.setSize(new Dimension(50, 40));
			btnExit.setLocation(428, 449);
			btnExit.setFont(new Font("Microsoft New Tai Lue ", Font.BOLD, 12));
			btnExit.setBorder(BorderFactory
					.createLineBorder(SystemColor.desktop));
			btnExit.addActionListener(this);
		}
		return btnExit;
	}

	private JButton getBtnAddDurestionTime() {
		if (btnAddDurastionTime == null) {
			btnAddDurastionTime = new JButton(
					" send request to Add Durestion time");
			btnAddDurastionTime.setForeground(Color.RED);
			btnAddDurastionTime.setFont(new Font("Tahoma", Font.BOLD
					| Font.ITALIC, 12));
			btnAddDurastionTime.setBounds(53, 345, 282, 40);
			btnAddDurastionTime.addActionListener(this);
		}
		return btnAddDurastionTime;
	}

	private JButton getBtnCreateExamCode() {
		if (btnCreateExamCode == null) {
			btnCreateExamCode = new JButton("Extarct Exam");
			btnCreateExamCode.addActionListener(this);
			btnCreateExamCode.setFont(new Font("Dialog", Font.BOLD, 12));
			btnCreateExamCode.setSize(240, 40);
			btnCreateExamCode.setBounds(53, 233, 282, 40);
		}
		return btnCreateExamCode;
	}

	private JButton getBtnLockExam() {
		if (btnLockExam == null) {
			btnLockExam = new JButton("Lock exam");
			btnLockExam.setBounds(345, 400, 107, 29);
			btnLockExam.addActionListener(this);
		}
		return btnLockExam;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == btnNewExam) {
			lecturerCtrl.handleLectutereGUI(LecturerController.NEWEXAM);
		}

		if (e.getSource() == btnPReports) {

			lecturerCtrl.handleLectutereGUI(LecturerController.REPORT);
		}
		if (e.getSource() == btnCreateExamCode) {
			lecturerCtrl.handleLectutereGUI(LecturerController.EXTRACTEXAM);
		}
		if (e.getSource() == btnExit) {
			lecturerCtrl.handleLectutereGUI(LecturerController.EXIT);
		}
		if (e.getSource() == btnCreateQuestion) {
			lecturerCtrl.handleLectutereGUI(LecturerController.NEWQUESTION);
		}
		if (e.getSource() == btnAddDurastionTime) {
			lecturerCtrl.handleLectutereGUI(LecturerController.ADDDURESTION);

		}
		if (e.getSource() == btnCheckExams) {
			lecturerCtrl.handleLectutereGUI(LecturerController.APPROVEGRADES);
		}

		if (e.getSource() == btnLockExam) {
			if ((examLst != null) || (!examLst.isEmpty())) {
				int comboindex = cmbBxLockExam.getSelectedIndex();
				if ((comboindex < examLst.size()) && (comboindex >= 0)) {
					lecturerCtrl.lockExam(examLst.get(comboindex));
				}
			}
		}
	}
/**
 * clean exam list
 */
	public void cleanExams() {
		if (examLst.size() > 0) {
			examLst.clear();
			cmbBxLockExam.removeAllItems();
			btnLockExam.setEnabled(false);
			cmbBxLockExam.setEnabled(false);
		}
	}
/**
 * remove the selected exam from combo
 */
	public void removeExam() {
		examLst.remove(cmbBxLockExam.getSelectedIndex());
		cmbBxLockExam.removeItemAt(cmbBxLockExam.getSelectedIndex());
		if (examLst.size() == 0) {
			btnLockExam.setEnabled(false);
			cmbBxLockExam.setEnabled(false);
		}
	}
/**
 * add exams to combo
 * @param unlckdExms 
 */
	public void addExams(ArrayList<ExtractedExam> unlckdExms) {
		this.examLst = new ArrayList<ExtractedExam>();

		this.examLst.addAll(unlckdExms);
		for (int i = 0; i < (unlckdExms.size()); i++)
			cmbBxLockExam.addItem(examLst.get(i).getExamCode() + " : "
					+ examLst.get(i).getSubjectID()
					+ examLst.get(i).getCourseID() + examLst.get(i).getExamID()
					+ " : " + examLst.get(i).getExamExtractDate());

		if (examLst.size() == 0) {
			btnLockExam.setEnabled(false);
			cmbBxLockExam.setEnabled(false);
		} else {
			btnLockExam.setEnabled(true);
			cmbBxLockExam.setEnabled(true);
		}
		cmbBxLockExam.addActionListener(this);
	}
}
